package ai4games_tests.west_world;

import java.util.ArrayList;

import game2dai.graph.Graph;
import game2dai.graph.GraphEdge;
import game2dai.graph.GraphNode;
import processing.core.PApplet;
import processing.core.PImage;

public class MakeGraphXML extends PApplet {

	PImage liberty;
	int INSIDE, OUTSIDE, BOTH_INSIDE, BOTH_OUTSIDE, IN_OUT;
	Graph graph;

	Integer[] loc;
	double delta = 40;


	public void setup(){
		size(640, 455);
		textSize(8);
		liberty = loadImage("ai4g/ww/liberty_03.png");
		image(liberty,0,0);

		liberty.loadPixels();
		int w = liberty.width;
		int h = liberty.height;
		int p[] = liberty.pixels;
		int x, y;
		graph = new Graph();

		INSIDE = 10;
		OUTSIDE = 1;
		BOTH_INSIDE = INSIDE + INSIDE;
		BOTH_OUTSIDE = OUTSIDE + OUTSIDE;
		IN_OUT = INSIDE + OUTSIDE;


		ArrayList<Integer> pos = new ArrayList<Integer>();

		int c = 0, col = 0;
		for(int i = 0; i < p.length; i++){
			col = (p[i] & 0x00ffffff);
			if(col == 0xff0000 || col == 0xffff00){
				if(col == 0xff0000)
					pos.add(OUTSIDE); // outside
				else
					pos.add(INSIDE);  // inside
				x = i % w;
				y = i / w;
				graph.addNode(new GraphNode(c, x, y));
				c++; // start IDs at 1
			}
		}
		loc = pos.toArray(new Integer[pos.size()]);

		// Add outside routes
		double d;
		for(int i = 0; i < c - 1; i++){
			for(int j = i + 1; j < c; j++){
				d = graph.distance(i, j);
				if(d < delta){
					int type = loc[i] + loc[j];
					if(type == BOTH_OUTSIDE ){
						graph.addEdge(i, j, d, d);
					}
				}
			}
		}

		// Add exit routes
		for(int i = 0; i < loc.length; i++){
			if(loc[i] == INSIDE){
				GraphNode insideNode = graph.getNode(i);
				GraphNode outsideNode = graph.getNodeNearest(insideNode);
				d = graph.distance(insideNode.id(), outsideNode.id());
				graph.addEdge(insideNode.id(), outsideNode.id(), 10*d, d);
			}
		}
				int[] e = new int[] { 4,11,  51,74,  81,82,  50,52,  43,44,  43,45,  54,57,  31,47,  8,21,  53,59,  49,53,  49,68,  9,19,  3,6,  71,75, 39,37 };
				for(int i = 0; i < e.length; i+=2)
					graph.addEdge(e[i], e[i+1], 0, 0);
		

		//		int[] r = new int[] { 40,47,  40,44 };
		//		for(int i = 0; i < r.length; i+=2){
		//			graph.removeEdge(r[i], r[i+1]);
		//			graph.removeEdge(r[i+1], r[i]);
		//		}
		//

		drawEdges();
		drawNodes();
		save("liberty_map.png");
	}

	public void drawEdges(){
		GraphEdge[] edges = graph.getEdgeArray();
		GraphNode start, end;
		stroke(1);
		stroke(0,255,255);
		for(GraphEdge edge : edges){
			start = edge.from();
			end = edge.to();
			line(start.xf(), start.yf(), end.xf(), end.yf());
		}
	}

	public void drawNodes(){
		int s = 4;
		fill(255,0,255);
		noStroke();
		GraphNode[] nodes = graph.getNodeArray();
		for(GraphNode node : nodes){
			fill(255);
			rect(node.xf()+3, node.yf() - 2*s, s*2.5f, 2.2f*s);
			if(loc[node.id()] == INSIDE){
				fill(0,255,0);
				System.out.println("Inside node  " + node.id());
			}
			else {
				fill(0,255,0);
			}
			fill(0);
			ellipse(node.xf(), node.yf(), s, s);
			text(node.id(), node.xf() + 3, node.yf());
		}
	}



}
